<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hexToRGB</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const hexToRGB = hex => {
            let alpha = false,
                h = hex.slice(hex.startsWith('#') ? 1 : 0);

            if (h.length === 3) {
                h = [...h].map(x => x.repeat(2)).join('');
            } else if (h.length === 8) {
                alpha = true;
            }

            h = parseInt(h, 16);

            const letterA = alpha ? 'a' : '';
            // r,g,b,a
            const r = h >>> (alpha ? 24 : 16);
            const g = (h & (alpha ? 0x00ff0000 : 0x00ff00)) >>> (alpha ? 16 : 8);
            const b = (h & (alpha ? 0x0000ff00 : 0x0000ff)) >>> (alpha ? 8 : 0);
            const a = alpha ? `,${h & 0x000000ff}` : '';

            return `rgb${letterA}(${r},${g},${b}${a})`;
        };
        console.log(hexToRGB('#27ae60ff'), hexToRGB('#fff'));
    </script>
</body>

</html>